package com.huizhonglm.diap.presenters.user;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.user.FinishUserInfoParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.user.iView.FinishUserInfoIView;

/**
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-11-19 11:25
 */
public class FinishUserInfoPresenter {

    private FinishUserInfoIView mView;

    public FinishUserInfoPresenter(FinishUserInfoIView view) {
        mView = view;
    }

    public void onSubmit(String name, String idcard, String phone, String codes) {
        FinishUserInfoParams params = new FinishUserInfoParams();
        params.userId = UserUtil.getUserId();
        params.userName = name;
        params.userIdCardNumber = idcard;
        params.userPhoneNumber = phone;
        params.userVerificationCode = codes;
        params.userBirthday = getBirthdayByIdcard(idcard);
        API.http().post("FinishUserInfo", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())) {
                        mView.onSubmitSuccess();
                    } else {
                        mView.onSubmitFailure();
                    }
                } else {
                    mView.onSubmitFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                mView.onSubmitFailure();
            }
        });
    }

    private String getBirthdayByIdcard(String idcard){
        String temp = idcard.substring(6, idcard.length() - 4);
        return temp.substring(0,4) + "-" + temp.substring(4,6) +"-"+temp.substring(6);
    }

}
